package net.qiqbframework.commandhandling.callbacks;


import net.qiqbframework.commandhandling.CommandCallback;
import net.qiqbframework.commandhandling.CommandMessage;
import net.qiqbframework.commandhandling.CommandResultMessage;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nonnull;


@Slf4j
public class FailureLoggingCallback<C, R> implements CommandCallback<C, R> {

    @Override
    public void onResult(@Nonnull CommandMessage<? extends C> commandMessage, @Nonnull CommandResultMessage<? extends R> commandResultMessage) {
        commandResultMessage.optionalExceptionResult()
                // log
                .ifPresent(cause -> log.error("Command '{}' resulted in {}({})", commandMessage.getCommandName(), cause.getClass().getName(), cause.getMessage(), cause));
    }
}
